home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / prcgntn1.lha / Precognition / source / AmigaMem.c next >
C/C++ Source or Header  |  1992-12-23  |  1KB  |  88 lines

  1. #include "AmigaMem.h"
  2. #include <exec/memory.h>
  3. #include <string.h>
  4. #include <proto/exec.h>
  5. #include "minmax.h"
  6.  
  7. void* Amalloc( ULONG size )
  8. {
  9.    ULONG *base;
  10.    
  11.    if (base = AllocMem( size+4, MEMF_CLEAR | MEMF_PUBLIC ))
  12.    {
  13.       base[0] = size+4;
  14.       return (void*) &base[1];
  15.    }
  16.    else
  17.       return NULL;
  18. }
  19.  
  20.  
  21. void* Acalloc( ULONG size, ULONG number )
  22. {
  23.    return Amalloc( size*number );
  24. }
  25.  
  26. char* Astrdup( const char* string )
  27. {
  28.    char* dup;
  29.    int len;
  30.    
  31.    if (string==NULL) return NULL;
  32.    
  33.    len = strlen( string );
  34.    
  35.    if (dup = Amalloc(len+1))
  36.    {
  37.       strcpy(dup,string);
  38.    }
  39.    return dup;
  40. }
  41.  
  42.  
  43. void Afree( void* mem )
  44. {
  45.    ULONG *base; 
  46.    
  47.    if (mem)
  48.    {
  49.       base = ((ULONG*)mem) -1;
  50.    
  51.       FreeMem( base, base[0] );
  52.    }
  53. }
  54.  
  55.  
  56. ULONG Asizeof( void* buf )
  57. {
  58.    ULONG *base; 
  59.    
  60.    if (buf)
  61.    {
  62.       base = ((ULONG*)buf) -1;
  63.       return base[0];
  64.    }
  65.    else
  66.       return 0;
  67. }
  68.  
  69. void *Arealloc( void *oldbuf, ULONG newsize )
  70. {
  71.    void *newbuf = Amalloc( newsize );
  72.    ULONG minsize, oldsize;
  73.    
  74.    if (newbuf)
  75.    {
  76.       oldsize = (oldbuf) ? Asizeof(oldbuf) : 0;
  77.       minsize = MIN(oldsize,newsize);
  78.    
  79.       memcpy( newbuf, oldbuf, minsize );
  80.       Afree(oldbuf);
  81.  
  82.       return newbuf;
  83.    }
  84.    else
  85.       return NULL; 
  86.  
  87. }
  88.